from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from Answer import Answer

class Unit2:
    def __init__(self, driver):
        self.Chapter2(driver)

    def Chapter2(self, driver):
        answer = Answer()
        # 点击第二单元的学习
        label = driver.find_element(By.CSS_SELECTOR, '#unit53 > div.book_unit_btn_div > button:nth-child(2)')
        driver.execute_script("arguments[0].click();", label)

        # 开始

        # 读取答案并填入
        file_name = 'Unit2Answer.txt'
        with open(file_name) as file_objECt:
            lines = file_objECt.readlines()

        # c1p2
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="chapter_menu1"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="chapter_menu1"]/li[2]/a').click()
        answers = [lines[0], lines[1], lines[2]]
        answer.vacancy_question(driver, answers, 1, 3)

        # c2t1n1
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="chapter137"]')))
        driver.find_element(By.XPATH, '//*[@id="chapter137"]').click()
        answers = lines[3].split()
        counts = [1, 1]
        answer.choice_question(driver, answers, 1, counts, 0)

        # c2t1n2
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[2]/a').click()
        answers = lines[4].split()
        counts = [2, 2, 1]
        answer.choice_question(driver, answers, 2, counts, 0)

        # c2t2c1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(2) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[5].split()
        counts = [1, 1, 1, 1, 1]
        answer.choice_question(driver, answers, 1, counts, 0)

        # c2t2c2
        WebDriverWait(driver, 5, 0.5).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[2]/a').click()
        answers = lines[6].split()
        counts = [2, 2, 2, 2]
        answer.choice_question(driver, answers, 2, counts, 0)

        # c2t3p1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(3) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[7], lines[8]]
        answer.vacancy_question(driver, answers, 1, 2)

        # c2t3p2
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[2]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[2]/a').click()
        answers = lines[9].split()
        counts = [1, 1, 1, 1, 1]
        answer.choice_question(driver, answers, 2, counts, 0)

        # c2t3p3
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(3) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[10].split()
        counts = [2, 2]
        answer.choice_question(driver, answers, 3, counts, 0)

        # c2t3p4
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="passage"]/li[4]/a')))
        driver.find_element(By.XPATH, '//*[@id="passage"]/li[4]/a').click()
        answers = lines[11].split()
        counts = [3, 3, 2, 2, 2]
        answer.choice_question(driver, answers, 4, counts, 0)

        # c2t3p5
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(5) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[12].split()
        counts = [4, 4]
        answer.choice_question(driver, answers, 5, counts, 0)

        # c2t3p6
        label = driver.find_element(By.CSS_SELECTOR, '#passage > li:nth-child(6) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = lines[13].split()
        counts = [5, 5, 3, 3, 3]
        answer.choice_question(driver, answers, 6, counts, 0)

        # c5
        label = driver.find_element(By.CSS_SELECTOR, '#chapter140')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[14], lines[15], lines[16], lines[17], lines[18], lines[19], lines[20], lines[21], lines[22], lines[23]]
        answer.vacancy_question(driver, answers, 1, 10)

        # c6
        WebDriverWait(driver, 5, 0.5).until(
            EC.visibility_of_element_located((By.XPATH, '//*[@id="chapter141"]')))
        driver.find_element(By.XPATH, '//*[@id="chapter141"]').click()
        answers = [lines[24], lines[25], lines[26], lines[27], lines[28], lines[29], lines[30], lines[31], lines[32], lines[33]]
        answer.vacancy_question(driver, answers, 1, 10)

        # c7t1
        label = driver.find_element(By.CSS_SELECTOR, '#chapter1443')
        driver.execute_script("arguments[0].click();", label)
        answer.submit(driver, 1)

        # c7t2
        label = driver.find_element(By.CSS_SELECTOR, '#chapter_menu1 > li:nth-child(2) > a')
        driver.execute_script("arguments[0].click();", label)
        answers = [lines[35], lines[36], lines[37], lines[38], lines[39],lines[40], lines[41], lines[42], lines[43], lines[44], lines[45]]
        answer.vacancy_question(driver, answers, 1, 11)

        driver.back()